<?php
	echo $this->element('admin_search_filter');
?>
<?php
echo "<div class='switcher'>";
echo __("Switch to ").$html->link("Listings",array("plugin"=>null,"controller"=>"listings","action"=>"index"));
echo "&nbsp;&nbsp;".$html->link("Review Comments",array('plugin'=>null,'controller'=>'review_comments','action'=>"index"));
echo "</div><br/>";

if(!empty($reviewData))
{
	$index->setData($reviewData,'Review');
	
	$index->showFields(array('id',array('created'=>'Date Added'),array('title'=>'Review Title'),array('listing_id'=>'Listing Name'),array('user_id'=>'Posted By')));
	$index->sortableFields(array('id','body'));

	$index->setAction(__('View'),'/admin/reviews/view/:Review.id');
	$index->setAction(__('Edit'),'/admin/reviews/edit/:Review.id');
	$index->setAction(__('Delete'),'/admin/reviews/delete/:Review.id',$this->helpers,__('Warning! Deleting this review will delete all associated comments. Are you sure you want to delete this review ?'));
	$index->setAction(__('Add meta information'),'/admin/meta_information/setInfo/Review/:Review.id',array('onclick'=>"openAsPopup(this);return false;"));
	$index->setAction(null,null,$this->helpers,false,true,'toggle_featured');
	$index->setAction(null,null,$this->helpers,false,true,'publish_unpublish');
	
	$index->addButton(__("Delete Selected"),array("action"=>"bulkDelete",'admin'=>true),array("onSubmit"=>"if(!confirm('".__("Are you sure?")."')){return false;}"));
	$index->addButton(__("Unpublish Selected"),array("action"=>"bulkUnpublish",'admin'=>true));
	$index->addButton(__("Publish Selected"),array("action"=>"bulkPublish",'admin'=>true));
	
	$index->addColumn('created','showFormatedDate');
	$index->addColumn('user_id','setPosterName');
	$index->addColumn('listing_id','setListingName');
	$index->addColumn('title','formatReviewTitle');

	function showFormatedDate($record)
	{
		return date('Y-m-d',strtotime($record['Review']['created']));
	}
	
	function setPosterName($record)
	{
		return $record['Review']['poster']['display_name'];
	}
	
	function setListingName($record)
	{
		return $record['Listing']['title'];
	}
	
	function formatReviewTitle($record)
	{
		uses('Flay');		
		return Flay::fragment($record['Review']['title'],20);		
	}
	
	function toggle_featured($model,$record)
	{
		if($record[$model]['is_featured'] == 1)
		{
			$action['title'] = __('Remove from featured');
			$action['url'] = '/admin/reviews/change_feature_status/'.$record[$model]['id'];
		}else
		{
			$action['title'] = __('Make it featured');
			$action['url'] = '/admin/reviews/change_feature_status/'.$record[$model]['id'];
		}
		return $action;
	}
	function publish_unpublish($model,$record)
	{
		if($record[$model]['status'] == Review::PUBLISHED)
		{
			$action['title'] = __('Unpublish');
			$action['url'] = '/admin/reviews/changeStatus/'.$record[$model]['id'];
			$action['confirmMessage'] = __('Warning! Unpublising this review will unpublish all associated comments. Are you sure you want to unpublish this review ?');
		}else
		{
			$action['title'] = __('Publish');
			$action['url'] = '/admin/reviews/changeStatus/'.$record[$model]['id'];
		}
		return $action;
	}
	$index->setPaginateSeparator(' ');
	echo $index->display();
	
}else
{
	echo "<h4>".__('No reviews found!')."</h4>";
}
?>